package myStack;

public class BracketIsMatch {
    public static boolean isMatch(String str) //判断算法
    {
        int i = 0;
        char e, x;
        MySequeStack<Character> st = new MySequeStack<Character>();
        //建立一个顺序栈
        while (i < str.length()) {
            e = str.charAt(i);
            if (e == '(' || e == '[' || e == '{') st.push(e);            //将左括号进栈
            else {
                if (e == ')') {
                    if (st.empty()) return false;        //栈空返回false
                    if (st.peek() != '(') return false;   //栈顶不匹配返回假
                    st.pop();
                }
                if (e == ']') {
                    if (st.empty()) return false;        //栈空返回false
                    if (st.peek() != '[') return false;   //栈顶不匹配返回假
                    st.pop();
                }
                if (e == '}') {
                    if (st.empty()) return false;        //栈空返回false
                    if (st.peek() != '{') return false;   //栈顶不匹配返回假
                    st.pop();
                }
            }
            i++;                        //继续遍历str
        }
        return st.empty(); //栈不空返回false
    }

    public static void main(String[] args)
    {  System.out.println("测试1");
        String str="([)}]";
        if (isMatch(str)) System.out.println(str+"中括号是匹配的");
        else System.out.println(str+"中括号不匹配");
        System.out.println("测试2");
        str="([])(){}";
        if (isMatch(str))
            System.out.println(str+"中括号是匹配的");
        else
            System.out.println(str+"中括号不匹配");
    }

}
